define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'vip/birthday/index' + location.search,
                    add_url: 'vip/birthday/add',
                    edit_url: 'vip/birthday/edit',
                    del_url: 'vip/birthday/del',
                    multi_url: 'vip/birthday/multi',
                    table: 'vip_birthday',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'username', title: __('Username')},
                        {field: 'images', title: __('Images'), events: Table.api.events.image, formatter: Table.api.formatter.images},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1')}, formatter: Table.api.formatter.status},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1')}, formatter: Table.api.formatter.toggle},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            // 批量修改js
            $(".btn-edit_all").click(function(){
                var ids = Table.api.selectedids(table);
                layer.confirm('确定要派发选中吗？', {
                    btn: ['确定','取消'] //按钮
                }, function(){
                    $.ajax({
                        url:"vip/birthday/edit_all/ids/"+ids,
                        dataType:"json",
                        type:"get",
                        //data:{"ids":ids},
                        success:function(re){
                            if(re.status == 200){
                                layer.msg(re.msg);
                                table.bootstrapTable('refresh', {});
                            } else {
                                layer.msg(re.msg);
                            }
                        }
                    });
                }, function(){
                    layer.msg('已取消', {icon: 2});
                });

            });

        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});